<template>
  <section class="welfare-coupon-main">
     <section class="main-search merge">
      <span class="svg-container">
        <svg-icon icon-class="search" />
      </span>
      <el-input v-model.trim="listQuery.name" placeholder="输入兑换券名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
      <section class="search-btn">
        <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
        <el-button type="primary" @click="handleReset">重置</el-button>
      </section>
    </section>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>兑换券管理</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="handleCreate">新建</el-button>
      </section>
    </section>
    <div class="classify-tab">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane v-for="item in dict.COUPONAUSTATUS" :key="item.dictValue" :label="item.dictName" :name="item.dictValue" />
      </el-tabs>
    </div>
    <section class="main-table">
      <v-table :list="list" tag="couponmain" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="activeTab === 'COUPONAUSTATUS01'" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="activeTab === 'COUPONAUSTATUS01' || activeTab === 'COUPONAUSTATUS00'" type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            <el-button v-if="activeTab === 'COUPONAUSTATUS03' && scope.row.COUPONSTATUS === 'COUPONSTATUS01'" type="text" size="mini" @click="handleDisabled(scope.row)">停用</el-button>
            <el-button v-if="activeTab === 'COUPONAUSTATUS01'" type="text" size="mini" @click="handleCheck(scope.row)">提交审核</el-button>
            <el-button v-if="activeTab === 'COUPONAUSTATUS02'" type="text" size="mini" @click="handleSyncCheck(scope.row)">同步审核结果</el-button>
          </template>
        </el-table-column>
      </v-table>
       <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
  </section>
</template>
<script>
import { queryPage, del, disabled, check, syncCheck, getDictByCode} from '../api'
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
export default {
  name: 'WelfareCoupon',
  components: { VTable, Pagination },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      list: [],
      total: 0,
      listLoading: false,
			activeTab: '',
			dict: {
				COUPONAUSTATUS: [],
			},
    }
  },
  created() {
		if(this.$route.query.ac){
			this.activeTab=this.$route.query.ac
		}
		for (const i in this.dict) {
			this.getDict(i)
		}
  },
  methods: {
		getDict(code) {
			return new Promise((resolve, reject) => {
				getDictByCode(code).then(response => {
					this.dict[code] = response.data.data[0].list
					if (code === 'COUPONAUSTATUS' && response.data.data[0].list.length > 0) {
						if(this.activeTab=='' ||this.activeTab==0){
								this.activeTab = response.data.data[0].list[0].dictValue
						}
						this.getList()
					}
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		},
		/**
		 * 选项卡切换
		 */
		handleTabClick(tab, event) {
			this.listQuery.page = 1
			this.getList()
		},
    filtercolumn(scope, item) {
      if (item === 'time') {
        return scope.row['startUseTime'] + ' 至 ' + scope.row['endUseTime']
      } else {
        return scope.row[item]
      }
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.name = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate() {
      this.$router.push({path:this.$filterPath("manage"), query: { t: 'c',ac:this.activeTab } })
    },

    handleUpdate(row) {
      this.$router.push({ path:this.$filterPath("manage"), query: { t: 'u', i: row.id ,ac:this.activeTab} })
    },
    /**
     * 同步审核结果
     */
		handleSyncCheck(row) {
			const loading = this.$loading({
				lock: true,
				text: '请稍后',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.3)'
			})
			return new Promise((resolve, reject) => {
				syncCheck({id: row.id}).then((res) => {
					loading.close()
          if(res.data.data && res.data.data.auditStatus) {
						this.$message({
							title: '成功',
							message: res.data.data.auditStatusValue || '操作成功',
							type: 'success',
							duration: 2000
						})
						this.activeTab = res.data.data.auditStatus
						this.getList()
          }
				}).catch(error => {
					loading.close()
					reject(error)
				})
			})
    },
		/**
     * 提交审核
		 */
		handleCheck(row) {
			this.$confirm('是否确认提交审核？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				const loading = this.$loading({
					lock: true,
					text: '请稍后',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.3)'
				})
				return new Promise((resolve, reject) => {
					check({id: row.id}).then(() => {
						loading.close()
						this.$message({
							title: '成功',
							message: '操作成功',
							type: 'success',
							duration: 2000
						})
						this.getList()
					}).catch(error => {
						loading.close()
						reject(error)
					})
				})
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消'
				})
			})
    },
    /**
     * 删除
		 */
		handleDelete(row) {
			this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				const loading = this.$loading({
					lock: true,
					text: '请稍后',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.3)'
				})
				return new Promise((resolve, reject) => {
					del(row.id).then(() => {
						loading.close()
						this.$message({
							title: '成功',
							message: '删除成功',
							type: 'success',
							duration: 2000
						})
						this.getList()
					}).catch(error => {
						loading.close()
						reject(error)
					})
				})
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				})
			})
    },
    /**
     * 停用
		 */
		handleDisabled(row) {
			this.$confirm('一旦停用则无法再次启用, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				const loading = this.$loading({
					lock: true,
					text: '请稍后',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.3)'
				})
				return new Promise((resolve, reject) => {
					disabled({id: row.id}).then(() => {
						loading.close()
						this.$message({
							title: '成功',
							message: '停用成功',
							type: 'success',
							duration: 2000
						})
						this.getList()
					}).catch(error => {
						loading.close()
						reject(error)
					})
				})
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消'
				})
			})
    },
    getList(obj) {
			this.listLoading = true
			var query = {
				page: obj ? obj.page : this.listQuery.page,
				pageSize: obj ? obj.limit : this.listQuery.limit,
				auditStatus: this.activeTab,
        name: this.listQuery.name
			}
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';
.welfare-coupon-main {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .classify-tab {
    width: 100%;
    margin-bottom: 10px;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;
    .el-tabs__nav-wrap::after {
      display: none;
    }
    .el-tabs__header {
      margin: 0;
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-bottom: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
      .num {
        @include font-color($mainColor);
      }
    }
  }
}
</style>
